<?php
namespace app\index\model;
use think\Image;
use think\Model;
use think\Db;

class Shareimg extends Model
{

    #
    public function getmakepic($positonModel,$user_id)
    {
        return $this->make($positonModel, $user_id);
    }


    public function  make($positonModel,$user_id){
        $selfimgs = $this->getStaticImg();
        $res = [];
        $qrcode = $this->generate($user_id, $positonModel->id);
        $name = $positonModel->title;
        $price = $positonModel->salary;
        $qrcode = publicPath().'/'.$qrcode;
        $nickName = User::find($user_id)->nickName;

        foreach ($selfimgs as $key=>$val) {
            $gbimg = publicPath().$val->bgimg;
            $res[] = $this->makePicture($key,  $gbimg,$name,$nickName, $price,$qrcode,$positonModel->id);
        }

        return $res;
    }

    public function makePicture($key,$gbimg, $positionName,$nickName,$price,$qrcode,$position_id)
    {
        $bgStatic = publicPath().'/'.'bg-1.png';
        $img = Image::open($bgStatic);
            $img->water($gbimg,[0,0]);
            $qrcodeHeight = 900;
            $fontPath = publicPath().'/font/simkai.ttf';
            $wecome = $positionName;
            $text = " -- 长按识别或扫描二维码进入 -- ";

            $img->water($qrcode,[120,1040]);
            $img->text($nickName, $fontPath,30,'#423f3f ',[350,1080] );
            $img->text($wecome, $fontPath,30,'#423f3f ',[350,1150] );
            $img->text($text, $fontPath,30,'#a6a6a6 ',  [50,1280]);
            //#a6a6a6
            $savePath = productPath($position_id)."/{$key}share_img.png";
            $img->save($savePath);
            $resPath = str_replace(publicPath(),'', $savePath);

        return $resPath;
    }


    public function getStaticImg()
    {
        $data = self::all();
        return $data;
    }


    /**
     * 生成二维码
     *
     * @return void
     */
    public function generate($uid, $pid)
    {
        require dirname(dirname(dirname(__DIR__))).'/vendor/phpqrcode/phpqrcode.php';
        //生成的地址
        $url = THEME.'/qrcode/'.$uid.'_'.$pid;
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 6;
        $root = $_SERVER['DOCUMENT_ROOT'];
        $path = 'uploads/down/';
        $filePath = $path.$uid.'_'.md5($url).'.png';

        //判断文件是否存在
        if (!is_dir($root.'/'.$path) ) {
            mkdir($root.'/'.$path, 0777, true);
        }
        //生成二维码图片
        \QRcode::png($url, $filePath, $level, $size);

        return $filePath;
    }





}
